package com.javaclimb.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaclimb.drug.common.DateUtil;
import com.javaclimb.drug.common.LogUtil;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.entity.Druginfo;
import com.javaclimb.drug.entity.Saleinfo;
import com.javaclimb.drug.entity.User;
import com.javaclimb.drug.service.IDruginfoService;
import com.javaclimb.drug.service.ISaleinfoService;
import com.javaclimb.drug.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 销售记录相关的controller
 */
@Controller
@RequestMapping(value = "/saleinfo")
public class SaleinfoController {

    @Autowired
    private IDruginfoService druginfoService;

    @Autowired
    private ISaleinfoService saleinfoService;

    @Autowired
    private LogService logService;
    /**
     * 转向销售记录页面
     */
    @RequestMapping
    public String saleinfo(){
        return "/saleinfo";
    }

    /**
     * 分页查询销售记录列表
     */
    @RequestMapping(value = "/saleinfoQueryPage")
    @ResponseBody
    public Object saleinfoQueryPage(String param, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpSession session){
        try{
            IPage<Saleinfo> iPage = saleinfoService.selectSaleinfoPage(page,limit,param);
            User user= (User) session.getAttribute("user");
            String message=user.getUsername()+":查询销售记录信息";
            logService.LogInfo(LogUtil.getLogInfo(message));
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向销售记录新增页面
     */
    @RequestMapping(value = "/saleinfoPage")
    public String saleinfoPage(){
        return "/saleinfoPageInsert";
    }

    /**
     * 添加一个销售记录
     */
    @RequestMapping(value = "/saleinfoAdd")
    @ResponseBody
    public Object saleinfoAdd(Saleinfo saleinfo,HttpSession session){
        try{
            User user= (User) session.getAttribute("user");
            saleinfo.setOperator(user.getUsername());
            Druginfo druginfo=druginfoService.queryDruginfoByName(saleinfo.getDname(),saleinfo.getDnumber());
            if(druginfo!=null)
                saleinfo.setTotal(saleinfo.getCount()*druginfo.getPrice());

            druginfo.setStock(druginfo.getStock()-saleinfo.getCount());
            druginfoService.updateStockById(druginfo);
            saleinfo.setOperatetime(new Date());
            int i = saleinfoService.addSaleinfo(saleinfo);

            String message=user.getUsername()+":销售药品"+saleinfo.getDname()+":数量   "+saleinfo.getDnumber()+"  操作时间："+ DateUtil.dateConvert(new Date());
            logService.LogInfo(LogUtil.getLogInfo(message));
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向销售记录编辑页面
     */
    @RequestMapping(value = "/saleinfoQueryById")
    public String saleinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        Saleinfo saleinfo = saleinfoService.querySaleinfoById(id);
        model.addAttribute("obj",saleinfo);
        return "/saleinfoPage";
    }

    /**
     * 修改一个销售记录
     */
    @RequestMapping(value = "/saleinfoEdit")
    @ResponseBody
    public Object saleinfoEdit(Saleinfo saleinfo,HttpSession session){
        try{
            User user= (User) session.getAttribute("user");
            saleinfo.setOperator(user.getUsername());
            Saleinfo oldSaleinfo=saleinfoService.querySaleinfoById(saleinfo.getId());
            Druginfo druginfo=druginfoService.queryDruginfoByName(saleinfo.getDname(),saleinfo.getDnumber());
            saleinfo.setTotal(saleinfo.getCount()*druginfo.getPrice());
            int number=oldSaleinfo.getCount()-saleinfo.getCount();
            if (number>=0){
                druginfo.setStock(druginfo.getStock()+Math.abs(number));
            }else {
                druginfo.setStock(druginfo.getStock()-Math.abs(number));
            }
            druginfoService.updateById(druginfo);
            saleinfo.setOperatetime(new Date());
            int i = saleinfoService.editSaleinfo(saleinfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个销售记录
     */
    @RequestMapping(value = "/saleinfoDelById")
    @ResponseBody
    public Object saleinfoDelById(Integer id,HttpSession session){
        try{
            Saleinfo saleinfo=saleinfoService.querySaleinfoById(id);
            int i = saleinfoService.delSaleinfoById(id);
            User user= (User) session.getAttribute("user");
            String message=user.getUsername()+":删除了"+saleinfo.getDname()+"的销售信息，药品质量出现问题";
            logService.LogInfo(LogUtil.getLogInfo(message));
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

}





















